Skip to main content

Python Comments

banner-python

In Python 3, comments are super useful for explaining your codeβ€”like sticky notes to your future self or teammates. Here's how to do both single-line and multi-line comments:

πŸ“ Single-line Comments​

Use the # symbol. Everything after # on that line is ignored by Python.

# This is a single-line comment
print("Hello, World!") # This prints a greeting

πŸ“ Multi-line Comments (Two Options)​

# This is a multi-line comment
# written across several lines.
# Each line starts with a '#'.
print("This works great!")

Option 2: Triple Quotes (''' or """) β€” a sneaky workaround​

Technically, triple quotes create a multi-line string, but if it's not assigned to anything, Python ignores it. So, it's used like a comment:

'''
This is a multi-line comment
using triple quotes.
Not technically a comment, but it works.
'''
print("Still works fine!")

"""
This is another way using double quotes.
Used for multi-line docstrings too.
"""

⚠️ Note: Use triple quotes for docstrings (i.e., documentation for functions/classes), and # for real comments. Triple-quoted "comments" are still technically strings, so overuse may affect performance slightly in large scripts.


βœ… Quick Summary​

TypeSyntax
Single-line# This is a comment
Multi-line# Line 1\n# Line 2
Alt Multi-line''' Text ''' or """ Text """

Want help commenting part of your code? Just share itβ€”I can help πŸ˜€